
load('summary_data/figs7_8_A12_A13_A14.rda')
source('utils.R')

c2 = render_basic(c2) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 2, .5), seq(3, 9, 1))) +
  scale_shape_manual(name = 'Gender',
                     values = c(20, 4),
                     labels = c('F', 'M')) +
  labs(
    x = element_blank(),
    y = '% Change from 2012 Copartisan',
    color = element_blank(),
    subtitle = 'Gender (Women, Men)'
  )

c10 = render_basic(c10) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 2, .5), seq(3, 9, 1))) +
  scale_shape_manual(
    name = 'Education',
    values = c(20, 4),
    labels = c('Non-Col. White', 'College White')
  ) +
  labs(
    x = element_blank(),
    y = '% Change from 2012 Copartisan',
    color = element_blank(),
    subtitle = 'Education among Whites (Non-College, College)'
  )

c12 = render_basic(c12) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 2, .5), seq(3, 9, 1))) +
  scale_shape_manual(name = 'Ethnicity',
                     values = c(20, 4),
                     labels = c('NW', 'W')) +
  labs(
    x = element_blank(),
    y = '% Change from 2012 Copartisan',
    color = element_blank(),
    subtitle = 'Ethnicity (POC, White)'
  )

legend = c2 %+%
  subset(c2$data, variable %in% c('Biden', 'Trump20')) +
  scale_color_manual(
    values = c('darkslateblue', 'darkred') ,
    labels = c('Biden (2020)', 'Trump (2020)'),
    breaks = c('Biden', 'Trump20')
  ) +
  guides(shape = 'none', color = guide_legend(reverse = T)) +
  theme(legend.position = 'bottom')

legend = get_legend_35(legend)

c2_20 = c2 %+%
  subset(c2$data, variable %in% c('Biden', 'Trump20')) +
  scale_color_manual(
    values = c('darkslateblue', 'darkred') %>% rev(),
    labels = c('Biden (2020)', 'Trump (2020)') %>% rev()
  ) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 3, .5), seq(3, 8, 1))) +
  coord_cartesian(ylim = c(-1, 5.25)) +
  guides(color = 'none', shape = guide_legend(nrow = 1)) +
  scale_shape_manual(
    name = element_blank(),
    values = c(20, 4),
    labels = c('Women', 'Men')
  ) +
  labs(subtitle = element_blank(), shape = element_blank()) +
  theme(plot.margin = margin(t = -0.5, 1, 1, 1))

c10_20 = c10 %+%
  subset(c10$data, variable %in% c('Biden', 'Trump20')) +
  scale_color_manual(
    values = c('darkslateblue', 'darkred') %>% rev(),
    labels = c('Biden (2020)', 'Trump (2020)') %>% rev()
  ) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 3, .5), seq(3, 8, 1))) +
  coord_cartesian(ylim = c(-1, 5.25)) +
  guides(color = 'none', shape = guide_legend(nrow = 1)) +
  scale_shape_manual(
    name = element_blank(),
    values = c(20, 4),
    labels = c('Non-College White', 'College White')
  ) +
  labs(subtitle = element_blank(),
       y = element_blank(),
       shape = element_blank()) +
  theme(plot.margin = margin(t = -0.5, 1, 1, 1))

c12_20 = c12 %+%
  subset(c12$data, variable %in% c('Biden', 'Trump20')) +
  scale_color_manual(
    values = c('darkslateblue', 'darkred') %>% rev(),
    labels = c('Biden (2020)', 'Trump (2020)') %>% rev()
  ) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 3, .5), seq(3, 8, 1))) +
  coord_cartesian(ylim = c(-1, 5.25)) +
  guides(color = 'none', shape = guide_legend(nrow = 1)) +
  scale_shape_manual(
    name = element_blank(),
    values = c(20, 4),
    labels = c('POC', 'White')
  ) +
  labs(subtitle = element_blank(),
       y = element_blank(),
       shape = element_blank()) +
  theme(plot.margin = margin(t = -0.5, 1, 1, 1))

plot = plot_grid(
  c2_20 + guides(shape = 'none'),
  c10_20 + guides(shape = 'none'),
  c12_20 + guides(shape = 'none'),
  nrow = 1
)

plot = plot_grid(plot, legend, rel_heights = c(9, .35), nrow = 2)

shapes = plot_grid(get_legend_35(c2_20),
                   get_legend_35(c10_20),
                   get_legend_35(c12_20),
                   nrow = 1)

plot = plot_grid(shapes, plot, rel_heights = c(.5, 9), nrow = 2)

ggsave(filename = 'figures/fig8.pdf', plot = plot, width = 8, height = 5, units = 'in')
